package cn.lzj.basic.controller;

import cn.lzj.basic.dto.SmsCodeDto;
import cn.lzj.basic.exception.BusinessException;
import cn.lzj.basic.service.IVerifyCodeService;
import cn.lzj.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    private IVerifyCodeService verifyCodeService ;
    /**
     * 发送图形验证码.
     * @param key  浏览器唯一key
     * @return
     */
    @GetMapping("/image/{key}")
    public AjaxResult imageCode(@PathVariable("key") String key){
        try {
            //生成base64编码之后的图片string
            String base64Str = verifyCodeService.imageCode(key);
            //将base64编码之后的图片string
            return AjaxResult.util().setResultObj(base64Str);
        } catch (BusinessException e) {//捕获我们自己的业务异常
            e.printStackTrace();
            return AjaxResult.util().setmessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.util().setmessage("系统繁忙，稍后重试！！");
        }
    }
    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody SmsCodeDto smsCodeDto){
        try {
            //生成base64编码之后的图片string
            verifyCodeService.smsCode(smsCodeDto);
            //将base64编码之后的图片string
            return new AjaxResult();
        } catch (BusinessException e) {//捕获我们自己的业务异常
            e.printStackTrace();
            return AjaxResult.util().setsuccess(false).setmessage("发送失败");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.util().setmessage("系统繁忙，稍后重试！！");
        }
    }
    @PostMapping("/bindersmsCode")
    public AjaxResult bindersmsCode(@RequestBody SmsCodeDto smsCodeDto){
        try {
            //生成base64编码之后的图片string
            verifyCodeService.bindersmsCode(smsCodeDto);
            //将base64编码之后的图片string
            return new AjaxResult();
        } catch (BusinessException e) {//捕获我们自己的业务异常
            e.printStackTrace();
            return AjaxResult.util().setsuccess(false).setmessage("发送失败");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.util().setmessage("系统繁忙，稍后重试！！");
        }
    }
}
